//package com.iwhalecloud.bss.kite.cucc.common.mybatis;
//
//import com.alibaba.druid.sql.ast.SQLStatement;
//import com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser;
//import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor;
//import com.alibaba.druid.sql.parser.SQLStatementParser;
//import com.alibaba.druid.stat.TableStat;
//import com.iwhalecloud.bss.kite.common.constant.SymbolConsts;
//import com.iwhalecloud.bss.kite.common.datasource.DataSourceName;
//import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
//import com.iwhalecloud.bss.kite.cucc.common.datasource.DataSourceRouter;
//import com.ztesoft.bss.common.util.PropertiesUtil;
//import org.apache.ibatis.cache.CacheKey;
//import org.apache.ibatis.executor.Executor;
//import org.apache.ibatis.mapping.BoundSql;
//import org.apache.ibatis.mapping.MappedStatement;
//import org.apache.ibatis.plugin.Interceptor;
//import org.apache.ibatis.plugin.Intercepts;
//import org.apache.ibatis.plugin.Invocation;
//import org.apache.ibatis.plugin.Plugin;
//import org.apache.ibatis.plugin.Signature;
//import org.apache.ibatis.session.ResultHandler;
//import org.apache.ibatis.session.RowBounds;
//
//import java.util.Arrays;
//import java.util.List;
//import java.util.Properties;
//import java.util.Set;
//
///**
// * 拦截判断是否使用Spring的事务管控
// */
//@Intercepts({
//    @Signature(type = Executor.class, method = "update", args = {
//        MappedStatement.class, Object.class
//    }),
//    @Signature(type = Executor.class, method = "query", args = {
//        MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class
//    }),
//    @Signature(type = Executor.class, method = "query", args = {
//        MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class, CacheKey.class, BoundSql.class
//    })
//})
//public class TranInterceptor implements Interceptor {
//
//    @Override
//    public Object intercept(Invocation invocation) throws Throwable {
//        //        if (!TransactionSynchronizationManager.isSynchronizationActive()) {
//        //            ErrorConsts.TRANSACTION_ERROR.throwOut();
//        //        }
//
//        String cfgTableName = PropertiesUtil.getProperty("config_table_name");
//        if (KiteStringUtils.isBlank(cfgTableName)) {
//            return invocation.proceed();
//        }
//        List<String> cfgTableNameList = Arrays.asList(cfgTableName.split(SymbolConsts.COMMA));
//        MappedStatement mappedStatement = (MappedStatement)invocation.getArgs()[0];
//        Object parameter = null;
//        if (invocation.getArgs().length > 1) {
//            parameter = invocation.getArgs()[1];
//        }
//
//        BoundSql boundSql = mappedStatement.getBoundSql(parameter);
//        String sql = boundSql.getSql();
//        // durid的sql解析器解析出表名
//        SQLStatementParser parser = new MySqlStatementParser(sql);
//        SQLStatement statement = parser.parseStatement();
//        MySqlSchemaStatVisitor visitor = new MySqlSchemaStatVisitor();
//        statement.accept(visitor);
//        Set<TableStat.Name> nameSet = visitor.getTables().keySet();
//
//        if (nameSet.stream().anyMatch(name -> cfgTableNameList.contains(name.getName()))) {
//            try {
//                DataSourceRouter.setDataSourceName(DataSourceName.dataSourceAppConfig);
//                return invocation.proceed();
//            }
//            finally {
//                DataSourceRouter.popDataSourceName();
//            }
//        }
//
//        return invocation.proceed();
//    }
//
//    @Override
//    public Object plugin(Object target) {
//        return Plugin.wrap(target, this);
//    }
//
//    @Override
//    public void setProperties(Properties properties) {
//
//    }
//}
